package top.wuyongshi.service.impl;
/**
 * Created by 郑明亮 on 2017/10/10 16:27.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import top.wuyongshi.model.CommonVO;
import top.wuyongshi.model.PageInfo;
import top.wuyongshi.model.TaskEntity;
import top.wuyongshi.service.ITaskRPC;


/**
 * @author 郑明亮   @email 1072307340@qq.com
 * @version 1.0
 * @time 2017/10/10 16:27
 * @description <p> </p>
 */
public class TaskRPCImpl implements ITaskRPC {

    @Override
    public PageInfo<TaskEntity> queryTaskByName(String taskName, CommonVO commonVO) {
        return null;
    }

    @Override
    public PageInfo<TaskEntity> queryAll(CommonVO commonVO) {
        return getTask(commonVO);
    }

    @Override
    public List<TaskEntity> queryAll() {
        return getTask();
    }

   static final String frequentness[] = {"日报","月报","季报","半年报","年报"};


    @Override
    public List<String> querySourcePools() {
       return getSourcePools();
    }

    @Override
    public List<String> queryTargetPools() {
        return getTargetPools();
    }

    /**
     * 模拟作业数据
     * @return
     */
    public static List<TaskEntity> getTask(){
        List<TaskEntity> list = new ArrayList<>();
        Random random = new Random();
        initTask(list, frequentness[random.nextInt(1000) % 5]);
        return list;
    }

    private static void initTask(List<TaskEntity> list, String frequentness) {
        for (int i = 0; i < 30; i++) {
            TaskEntity task = new TaskEntity();
            task.setId(""+i);
            task.setTaskName("task"+ getRandomCode(4));
            task.setTaskDesc("作业描述"+ UUID.randomUUID().toString());
            task.setTaskCreator("郑明亮");
            task.setTaskFrequentness(frequentness);
            task.setLimitTime(10);
            list.add(task);
        }
    }

    public static PageInfo<TaskEntity> getTask(CommonVO commonVO){
        List<TaskEntity> list = new ArrayList<>();
        Random random = new Random();
        initTask(list, frequentness[random.nextInt(1000)%5]);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(list);
        pageInfo.setPageSize(commonVO.getPageSize());
        pageInfo.setTotal(30);
        pageInfo.setPages(((int) Math.ceil(30.0 / commonVO.getPageSize())));
        pageInfo.setPageNum(commonVO.getPageNo());
        return pageInfo;
    }
    public static List<String> getSourcePools() {
        List<String> soucePools = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            soucePools.add("源连接池"+(i+1));
        }
        return soucePools;
    }

    public List<String> getTargetPools() {
        List<String> targetPools = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            targetPools.add("目标连接池"+(i+1));
        }
        return targetPools;
    }

    public static String getRandomCode(int number) {
        String codeNum = "";
        int[] code = new int[3];
        Random random = new Random();
        for (int i = 0; i < number; i++) {
            int num = random.nextInt(10) + 48;
            int uppercase = random.nextInt(26) + 65;
            int lowercase = random.nextInt(26) + 97;
            code[0] = num;
            code[1] = uppercase;
            code[2] = lowercase;
            codeNum += (char) code[random.nextInt(3)];
        }
        System.out.println(codeNum);

        return codeNum;
    }
}
